<template>
  <erp-form-material-01
    v-model="tempValue"
    v-bind="$attrs"
    :form="tempForm"
    :extra="extraCom"
    :options="computedOptions"
    @value-change="handleModalValueChange"
    @update-id-field="handleModalUpdateField"
  />
</template>

<script>
import ErpFormMaterial01 from '@/components/erp/form/ErpFormMaterial01';
import {materialShapeList} from "../../../utils/erp/cloth";
/**
 * 表格中弹窗组件
 * @author maolin
 */
export default {
  name: "ErpTableModal01",
  components: {
    ErpFormMaterial01
  },
  model: {
    prop: "value",
    event: "value-change"
  },
  props: {
    /**
     * 表单对象
     * @public
     */
    form: {
      type: Object,
      default () {
        return {};
      }
    },
    /**
     * 选择的默认值
     * @public
     */
    value: {
      type: [String, Number],
      default: ''
    },
    /**
     * 额外配置
     * @public
     */
    extra: {
      type: Object,
      default: () => {
        return {};
      }
    },
    options: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data () {
    return {
      tempValue: null,
      tempForm: null,
    };
  },
  computed: {
    extraCom () {
      let defaultConfig = {};
      defaultConfig.template = 'var code = form.materialCode ? "[" + form.materialCode + "]" : ""; var name = form.brandName || ""; var specs = form.materialSpecs || ""; return name + specs';
      return Object.assign(defaultConfig, this.extra);
    },
    computedOptions () {
      return this.options;
    }
  },
  watch: {
    value: {
      handler(value) {
        this.tempValue = value;
      },
      immediate: true
    },
    form: {
      handler(form) {
        this.tempForm = form;
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    /**
     * 将值传递父组件
     * @param data
     * @public
     */
    handleModalValueChange (data) {
      this.$emit('value-change', data);
      let form = this.form;
      let gweight = "";
      if (materialShapeList[form.materialShape]) {
        if (materialShapeList[form.materialShape].id === 'PRODUCT') {
          gweight = form.mgWeightProduct;
        } else if (materialShapeList[form.materialShape].id === 'GREY_CLOTH') {
          gweight = form.mgWeightBlank;
        }
      }
      this.handleModalUpdateField({
        gweight: gweight
      });
    },
    /**
     * 将表单更新
     * @param data
     * @public
     */
    handleModalUpdateField (data) {
      if (this.tempForm) {
        let newForm = Object.assign(this.tempForm, data);
        this.$emit('update:form', newForm);
      }

    },
  }
};
</script>

<style scoped>

</style>
